package com.example.lcpractice.linkedlist;

import com.example.lcpractice.datastructure.LinkedList;
import com.example.lcpractice.datastructure.ListNode;

import java.util.Stack;

/**
 * @author hd
 * @date 2022/2/25 10:36
 */
public class PrintLinkedListReverse {
    public static void main(String[] args) {
        final ListNode node = LinkedList.getLinkedList();
        reverseByStack(node);
//        recursion(node);
    }

    public static void reverseByStack(ListNode head) {
        Stack<ListNode> stack = new Stack<>();
        while (head != null) {
            stack.push(head);
            head = head.getNext();
        }
        while (!stack.isEmpty()) {
            System.out.println(stack.pop().getVal());
        }


    }

    public static void recursion(ListNode head) {
        if (head != null) {
            if (head.getNext() != null) {
                recursion(head.getNext());
            }
            System.out.println(head.getVal());
        }
    }

}
